#!/usr/bin/env python
# -*- coding: utf-8 -*-
# ==================================================
# @File    : time
# @IDE     : PyCharm
# @Author  : liuxiangui
# @Date    : 2023/12/30 15:10
# @Describe: 
# ==================================================
import datetime
import time
from typing import Text


def timestamp_to_datatime(timestamp: int) -> Text:
    """
    时间戳转时间,以秒为单位，例如：1703917410884 -> 2023-12-30 14:23:30
    :param timestamp:
    :return: Text
    """
    dt = datetime.datetime.fromtimestamp(timestamp / 1000)
    # 将时间格式化为字符串
    formatted_time = dt.strftime('%Y-%m-%d %H:%M:%S')
    return formatted_time


def sub_timestamp_to_datetime(milliseconds: int) -> Text:
    """
    时间差（ms）转时分秒格式，例如：1234567 -> 0小时 20分钟 34秒
    :param milliseconds:
    :return:
    """
    # 将毫秒转换为秒
    seconds = milliseconds / 1000
    # 使用divmod()函数计算小时和剩余的秒数
    hours, remainder_seconds = divmod(seconds, 3600)
    minutes, seconds = divmod(remainder_seconds, 60)
    m, ms = divmod(milliseconds, 1000)
    return f"{int(hours)}小时 {int(minutes)}分钟 {int(seconds)}秒 {ms}毫秒"


def datetime_format_ymd_hms(days_from_today=0, from_start=2):
    """
    计算得到2023-12-30 14:23:30格式的时间
    :param days_from_today: 0
    :param from_start: 0 计算当天00:00:00; 1 计算当天23:59:59; 2 计算目前时间
    """
    now = int(time.time())
    time_needed = now + days_from_today * 86400
    time_array = time.localtime(time_needed)
    if from_start == 0:
        res_time_style = time.strftime("%Y-%m-%d", time_array) + "00:00:00"
    elif from_start == 1:
        res_time_style = time.strftime("%Y-%m-%d", time_array) + "23:59:59"
    elif from_start == 2:
        res_time_style = time.strftime("%Y-%m-%d %H:%M:%S", time_array)
    elif from_start == 3:
        res_time_style = time.strftime("%Y-%m-%d-%H-%M-%S", time_array)
    return res_time_style


if __name__ == '__main__':
    sub_timestamp_to_datetime(1234567)
